package com.caiyouxi.advertise.service.impl;

import com.caiyouxi.advertise.mapper.PageAddressMapper;
import com.caiyouxi.advertise.model.ChildBean;
import com.caiyouxi.advertise.model.FatherBean;
import com.caiyouxi.advertise.model.PageAddressBean;
import com.caiyouxi.advertise.service.PageAddressService;
import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PageAddressServiceImpl extends BaseService implements PageAddressService {
    @Autowired
    private PageAddressMapper pageAddressMapper;

    /**
     * 新增
     * */
    @Override
    public ResData add_page_address(PageAddressBean bean) {
        Integer address = 0;
        try {
            //添加页面
            Integer result = pageAddressMapper.add_page_address(bean);
            if(result == 1 && bean.getChild_name() != null){
                //查询当前id作为pid
                Integer current_id = pageAddressMapper.get_current_id();
                bean.setPid(current_id);
                //添加位置数据
                address = pageAddressMapper.add_address(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事物
        }


        return success(address);
    }

    /**
     * 删除
     * */
    @Override
    public ResData delete_page_address(PageAddressBean bean) {
        return success(pageAddressMapper.delete_page_address(bean));
    }


    /**
     * 转换页面所需数据
     * */
    @Override
    public ResData get_page_address(PageAddressBean bean) {
        QueryParams queryParams = new QueryParams();
        List<FatherBean> fatherBeanList  = new ArrayList<>();
        List<PageAddressBean> list = pageAddressMapper.get_page_address(bean);
        for(int i = 0; i < list.size(); i ++){
            Integer id = list.get(i).getId();
            Integer is_father = list.get(i).getIs_father();
            List<ChildBean> childBeanList = new ArrayList<>();
            FatherBean fatherBean = new FatherBean();
            if(is_father == 0){                                                 //判断是否为父类
                fatherBean.setPage_name(list.get(i).getName());                 //给父类赋值
                fatherBean.setId(list.get(i).getId());
                for(int y = 0; y < list.size(); y ++){
                    Integer pid = list.get(y).getPid();
                    if(id == pid){                                              //判断有没有child节点
                        ChildBean childBean = new ChildBean();
                        childBean.setAddress_name(list.get(y).getName());
                        childBean.setAddress_name(list.get(y).getName());       //设置ChildBean位置名称
                        childBean.setId(list.get(y).getId());                   //设置ChildBean id
                        childBean.setOnline_num(list.get(y).getNumafter());     //设置ChildBean上线数量
                        childBean.setTotal_num(list.get(y).getNum());           //设置ChildBean总数量
                        childBean.setPicturl_url(list.get(y).getPicture_url()); //设置ChildBean图片地址
                        childBeanList.add(childBean);                           //赋值给childBeanList
                    }
                }
                fatherBean.setChild(childBeanList);                             //给fatherbean赋值list
                fatherBeanList.add(fatherBean);
            }
        }
        queryParams.setList(fatherBeanList);
        return page_list(queryParams);
    }

    @Override
    public ResData get_current_id() {
        return success(pageAddressMapper.get_current_id());
    }
}
